/*
package com.atguigu.gmall.order.intercepter;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;


*/
/**
 * 拦截器 拦截从未服务出去的请求
 *//*

@Component
public class OrderIntercepter implements RequestInterceptor {
    */
/**
     * 在feign调用之前执行
     * @param template
     *//*

    @Override
    public void apply(RequestTemplate template) {
        //使用spring提供的RequestContextHolder 获取请求头中所有信息
        ServletRequestAttributes   requestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null){
            //判断 是否为空
            HttpServletRequest request = requestAttributes.getRequest();
            //获取所有请求体中的名字
            Enumeration<String> headerNames = request.getHeaderNames();
            //迭代
            while (headerNames.hasMoreElements()){
                //迭代获取每个name
                String name = headerNames.nextElement();
                //根据那么获取对应的值
                String value = request.getHeader(name);

            }
        }
    }
}
*/
package com.atguigu.gmall.order.intercepter;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * 订单微服务的拦截器
 * 过滤器: Servlet组件--->进入微服务的请求
 * 拦截器: SpringMVC--->从微服务出去的请求
 */
@Component
public class OrderIntercepter implements RequestInterceptor {

    /**
     * 触发时机: feign调用发生前
     * @param requestTemplate
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取原始的请求对象
        ServletRequestAttributes requestAttributes =
                (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(requestAttributes != null){
            //获取原始请求对象的请求体
            HttpServletRequest request = requestAttributes.getRequest();
            //获取原始请求体的请求头中的所有的参数
            Enumeration<String> headerNames = request.getHeaderNames();
            //迭代
            while (headerNames.hasMoreElements()){
                //获取每个请求头的参数的名字
                String name = headerNames.nextElement();
                //获取值
                String value = request.getHeader(name);
                //存储到feign的http请求头中去
                requestTemplate.header(name, value);
            }
        }
    }
}

